/**
 * 设置PLC参数
 * @author xiaoxujs
 */

import { Form, Input, message, Modal } from "antd";
import { FC, useEffect, useState } from "react";
import { IYckzZyLoadControlHetGlj, postYckzyZLoadControlSetGlj } from "../../../api";

const SetPlcModule: FC<{
  open: boolean,
  handleSetGeiliaojiCallback: () => void;
  clickData?: IYckzZyLoadControlHetGlj,
  clickId?: string;
}> = ({
  open,
  handleSetGeiliaojiCallback,
  clickData,
  clickId
}) => {

    const [form] = Form.useForm<IYckzZyLoadControlHetGlj>();

    const [loading, setLoading] = useState(false);

    useEffect(() => {
      if (clickData) {
        form.setFieldsValue(clickData)
      }
    }, [clickData, form])
    const handleOk = async () => {
      const values = await form.validateFields();
      console.log(values)
      console.log(clickData)
      console.log(clickId)
      if ( clickId) {
        setLoading(true);
        const { code, msg } = await postYckzyZLoadControlSetGlj({
          ...values,
          controlId: clickId
        }) as { code: number; msg: string };
        if (code === 20000) {
          form.resetFields();
          handleSetGeiliaojiCallback();
        } else {
          message.error(msg)
        }
        setLoading(false)

      }

    };

    const handleCancel = () => {
      handleSetGeiliaojiCallback();
    };
    return <Modal title="设置给料机" confirmLoading={loading} open={open} onOk={handleOk} onCancel={handleCancel}>
      <Form
        labelCol={{ span: 8 }}
        wrapperCol={{ span: 14 }}
        form={form}
      >
        <Form.Item<IYckzZyLoadControlHetGlj>
          label="给料机1名称"
          name="glj1"
          rules={[{ required: true, message: '请输入给料机1名称!' }]}
        >
          <Input />
        </Form.Item>
        <Form.Item<IYckzZyLoadControlHetGlj>
          label="给料机1编号"
          name="glj1Num"
          rules={[{ required: true, message: '请输入给料机1编号!' }]}
        >
          <Input />
        </Form.Item>
        <Form.Item<IYckzZyLoadControlHetGlj>
          label="给料机2名称"
          name="glj2"
          rules={[{ required: true, message: '请输入给料机2名称!' }]}
        >
          <Input />
        </Form.Item>
        <Form.Item<IYckzZyLoadControlHetGlj>
          label="给料机2编号"
          name="glj2Num"
          rules={[{ required: true, message: '请输入给料机2编号!' }]}
        >
          <Input />
        </Form.Item>
        
      </Form>
    </Modal>
  }

export default SetPlcModule;